package com.gzr.demo01;

/**
 * 并发问题
 * 多个线程同时操作同一个对象
 * 买火车票的例子
 */
public class TestRunnable3 implements Runnable{

    private int ticketNums = 10;

    @Override
    public void run() {
        while (true) {
            if (ticketNums <= 0) {
                break;
            }

            //模拟延时
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNums--+"张票");
        }

    }

    public static void main(String[] args) {
        TestRunnable3 runnable3 = new TestRunnable3();

        new Thread(runnable3,"张飞").start();
        new Thread(runnable3,"刘备").start();
        new Thread(runnable3,"关羽").start();
    }
}
